<?php
			ob_start();			
?>
<?php
		
		if (isset($_COOKIE["user"])) {
			// if cookie <> ""
		  }
		  else
		  {
			  header("location:login.php");
			  
		  }
?>
<?php
function updatePublish($published)
{	
	$published = $published == 1 ? 0 : 1;
	$act_id_pub = $_REQUEST["act_id_pub"];
	$sql = "UPDATE tbl_activity SET published={$published} WHERE act_id=$act_id_pub";
	query($sql);	
}
?>
<?php
if(isset($_REQUEST["published"]))
{
	updatePublish($_REQUEST["published"]);
	
}
?>
<?php
function deleteRecord($id_imagename)
{
	//get id from array
	$act_id = key($id_imagename);
	//get cat_image from array
	$imagename = current($id_imagename);
	$sql = "DELETE FROM  tbl_activity WHERE act_id={$act_id}";
	$result = query($sql);	
	if($result)
	{
		//delete image product
		deleteFile("activity/{$imagename}");
		
		
		errorMessage("Supprimer réussies!");
	}	
	else
	{
		errorMessage("Insérez réussies!");
	}	
}
?>
<?php
	if(@is_array($_REQUEST["act_id"]))
{
	$id_imagename = $_REQUEST["act_id"];
	deleteRecord($id_imagename);
}
?>

                	<div id="border_cont_title">
                        	<div id='content_title_left'>gestion actualite</div>
                           <div id='content_title_right'><a href="index.php?page=actualityNew">Insérer actualite</a></div>
					</div> <!--end border_cont_title-->
                    
                    <?php echo @$errors;?>
                    	<!--start table -->
                            <table width="770px">
                            	<tr>
                                	<th>Photo</th>
                                	<th>Titre</th>
                                    <th>Teneur</th>
                                    <th>Date du post</th>
                                    <th>Publier</th>
                                    <th>Action</th>
                                </tr>
                              
                                <?php 
									//include function from cls_pag
									include('../includes/cls_pagination.php');
									
                                    $sql = "SELECT * from  tbl_activity"; 
                                    $result = query($sql);
									$Num_Rows = mysql_num_rows($result);
							
									$Per_Page = 15;   // Per Page
									
									@$Page = $_GET["Page"];
									if(!@$Page)
									{
										$Page=1;
									}
									
									$Prev_Page = $Page-1;
									$Next_Page = $Page+1;
									
									$Page_Start = (($Per_Page*$Page)-$Per_Page);
									if($Num_Rows<=$Per_Page)
									{
										$Num_Pages =1;
									}
									else if(($Num_Rows % $Per_Page)==0)
									{
										$Num_Pages =($Num_Rows/$Per_Page) ;
									}
									else
									{
										$Num_Pages =($Num_Rows/$Per_Page)+1;
										$Num_Pages = (int)$Num_Pages;
									}
									
									$sql .=" ORDER BY act_id DESC  LIMIT $Page_Start , $Per_Page";
									$result  = mysql_query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                ?>
                                <tr>
                                	<td align="center" width="100"><img src="actuality/<?php echo $row["photo"];?>"  width="70" height="70"/></td>
                                    <td align="left" width="100"><?php echo $row["act_title"];?></td>
                                    <td align="left" width="220">
                                        <?php 
											 $countSting=$row["message"];
											 if(strlen($countSting)<=20)
											 {
												  echo $sms=$countSting;
											 }
											 else
											 {
												 echo $sms=substr($countSting,0,150) ."...";
											 }
										?>
                                    </td>
                                    <td align="center"><?php echo $row["date_post"];?> </td>
                                    <td align="center">
									<?php 
                                            $icon_publish = $row["published"]==0 ? "publish_x.png" : "publish_y.png";
                                            $publish_image = "<img width='15px' src='images/{$icon_publish}' />";														
                                     ?>
                                            <a href="index.php?page=actuality&published=<?php echo $row["published"] . "&act_id_pub=" . $row["act_id"];?>">
                                            <?php echo $publish_image; ?></a>
                                   </td>
                                    <td align="center">
                                       <a href="index.php?page=actualityEdit&act_id=<?php echo $row["act_id"]?>"><img class="action_distant" src="images/edit.png"  /></a>
                                       <a href="index.php?page=actuality&act_id[<?php echo $row["act_id"];?>]=<?php echo $row["photo"];?>" onclick="return confirm('Êtes-vous sûr de vouloir supprimer ces nouvelles?');"><img class="action_distant" src="images/delete.png" /></a>
                                    </td>
                                </tr>
                                <?php
									}//end while

                                ?>
                            </table>
                         <!--end table -->
                    			<?php
								echo "<div id='Paginator_left'> Total $Num_Rows Record ";

                                    $pages = new Paginator;
                                    $pages->items_total = $Num_Rows;
                                    $pages->mid_range = 15;
                                    $pages->current_page = $Page;
                                    $pages->default_ipp = $Per_Page;
                                    //$pages->url_next = $_SERVER["PHP_SELF"]."?QueryString=value&Page=";
									$pages->url_next = "index.php?page=actuality&QueryString=value&Page=";
                                    $pages->paginate();
                                    
                                    echo $pages->display_pages();
									
                                echo " </div>" ;
							
                                ?>
                 